package com.phone580.app.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.phone580.app.BaseActivity;
import com.phone580.app.BaseTask.TaskCallback;
import com.phone580.app.R;
import com.phone580.app.entity.LoginEntity;
import com.phone580.app.register.RegisterActivity;
import com.phone580.app.untilty.CommonUtility;
import com.phone580.app.untilty.StringUtil;

public class LoginActivity extends BaseActivity implements TaskCallback{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		initHeader("登录");
		Button btnLogin = (Button) findViewById(R.id.login_btn);
		final CheckBox cbRemember = (CheckBox) findViewById(R.id.login_remember_pwd_cb);
		final EditText etUserName = (EditText) findViewById(R.id.user_et);
		final EditText etPwd = (EditText) findViewById(R.id.pwd_et);
		btnLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {	
				String username = etUserName.getText().toString();
				String pwd = etPwd.getText().toString();
				if (StringUtil.isNullOrEmpty(username)) {
//					etUserName.setError("请输入账号");
					Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
					return;
				}
				if (StringUtil.isNullOrEmpty(pwd)) {
//					etPwd.setError("请输入密码");
					Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if (cbRemember.isChecked()) {
					CommonUtility.storeValue(LoginActivity.this, "remember", "true");
					CommonUtility.storeValue(LoginActivity.this, "username", username);
					CommonUtility.storeValue(LoginActivity.this, "pwd", pwd);
				} else {
					CommonUtility.storeValue(LoginActivity.this, "remember", "false");
					CommonUtility.storeValue(LoginActivity.this, "username", "");
					CommonUtility.storeValue(LoginActivity.this, "pwd", "");
				}
				
				LoginTask task = new LoginTask(LoginActivity.this);
				task.setParams(username, pwd);
				task.setTaskCallback(LoginActivity.this);
				task.execute();
			}
		});
		
		final TextView tvReg = (TextView) findViewById(R.id.login_reg_tv);
		tvReg.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
		
		String value = CommonUtility.getStoreValue(this, "remember");
		if (value != null && "true".equals(value)) {
			cbRemember.setChecked(true);
			etUserName.setText(CommonUtility.getStoreValue(this, "username"));
			etPwd.setText(CommonUtility.getStoreValue(this, "pwd"));
		} else {
			cbRemember.setChecked(false);
			etUserName.setText(CommonUtility.getStoreValue(this, ""));
			etPwd.setText(CommonUtility.getStoreValue(this, ""));
		}

		
		cbRemember.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				String username = etUserName.getText().toString();
				String pwd = etPwd.getText().toString();
				if (isChecked) {
					CommonUtility.storeValue(LoginActivity.this, "remember", "true");
					CommonUtility.storeValue(LoginActivity.this, "username", username);
					CommonUtility.storeValue(LoginActivity.this, "pwd", pwd);
				} else {
					CommonUtility.storeValue(LoginActivity.this, "remember", "false");
					CommonUtility.storeValue(LoginActivity.this, "username", "");
					CommonUtility.storeValue(LoginActivity.this, "pwd", "");
				}
			}
		});
	}

	@Override
	public void execute(Object result) {
		if (result != null) {
			LoginEntity entity = (LoginEntity) result;
			if (entity.isSuccess()) {
				Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(this, entity.getMessage(), Toast.LENGTH_LONG).show();
			}
		} else {
			showNetworkError();
		}
		
	}
}
